package com.hazardSoft.eStore.models;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

public class ModelManagerProducts {
		private String name;
		private String description;
		private int brandSelected;
		private int categorySelected;
		private Double cost;
		private byte[] image;
		
		private int render;
		private String mime;
		
		public ModelManagerProducts()
		{
			render=0;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getDescription() {
			return description;
		}
		public void setDescription(String description) {
			this.description = description;
		}
		public int getBrandSelected() {
			return brandSelected;
		}
		public void setBrandSelected(int brandSelected) {
			this.brandSelected = brandSelected;
		}
		public int getCategorySelected() {
			return categorySelected;
		}
		public void setCategorySelected(int categorySelected) {
			this.categorySelected = categorySelected;
		}
		public Double getCost() {
			return cost;
		}
		public void setCost(Double cost) {
			this.cost = cost;
		}
		public byte[] getImage() {
			return image;
		}
		public void setImage(byte[] image) {
			this.image = image;
		}
		public int getRender() {
			return render;
		}
		public void setRender(int render) {
			this.render = render;
		}
		
		public String getMime() {
			return mime;
		}

		public void setMime(String mime) {
			this.mime = mime;
		}

		public void listener(UploadEvent event){		
	        UploadItem item = event.getUploadItem();
	        mimeType(item.getFileName());        
	        try {
				FileInputStream inputStream=new FileInputStream(item.getFile());
				if(item.getFileSize()<800001)
				{
					image=new byte[item.getFileSize()];
					inputStream.read(image);
					render=1;
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }  
		public void paint(OutputStream stream, Object object) throws IOException {
		   if(Integer.parseInt(object.toString())==1)
		   {
			   render=0;
			   stream.write(image);		   
		   }
		   else
		   {
			   stream.write(0);
			   image=null;
		   }
	    }
		
		public void mimeType(String fileName) {
			String name = fileName;
			int extDot = name.lastIndexOf('.');
			if(extDot > 0){
				String extension = name.substring(extDot +1);
				if("bmp".equals(extension)){
					mime="image/bmp";
				} else if("jpg".equals(extension)){
					mime="image/jpg";
				} else if("gif".equals(extension)){
					mime="image/gif";
				} else if("png".equals(extension)){
					mime="image/png";
				} else {
					mime = "image/unknown";
				}
			}
		}

		public void limpiar() {
			 name="";
			 description="";
			 brandSelected=0;
			 categorySelected=0;
			 cost=0.0;
			 image=new byte[0];
			
			 render=0;
			 mime="";
			
		}
		
		
}
